package com.wutianyi.init;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.lang.StringUtils;

public class InitCompanyStandardProperties {

	public static void main(String[] args) throws IOException, SQLException {
		Connection conn = DriverManager.getConnection(
				"jdbc:mysql://localhost:3307/test?characterEncoding=utf8",
				"root", "860728");
		InputStream inputStream = InitCompanyStandardProperties.class
				.getResourceAsStream("/company/公司信息组成");
		BufferedReader br = new BufferedReader(new InputStreamReader(
				inputStream));
		String line = br.readLine();
		String sql = "insert into config(type, gmt_create, gmt_modified, name, p_id)values(1,now(),now(),";
		int id = 0;
		int pId = 0;
		while (StringUtils.isNotBlank(line)) {
			Statement stmt = conn.createStatement();
			if (line.startsWith("\t")) {

				String s = sql + "'" + line.trim() + "'," + pId + ")";
				stmt.executeUpdate(s);
			} else {
				String s = sql + "'" + line.trim() + "'," + 0 + ")";
				stmt.executeUpdate(s);
				Statement sm = conn.createStatement();
				ResultSet rs = sm.executeQuery("select last_insert_id()");
				if (rs.next()) {
					pId = rs.getInt(1);
				}
			}

			line = br.readLine();
		}
	}
}
